<?php

namespace Home\Model\Ajax;

class UserRegSave extends AbstractAjax
{
    public function handle()
    {
        $SMSopen  = C('YTATA_SMS_OPEN');
        $username = I('request.username', '', 'trim');
        if (!$username) {
            $username = D('User')->generate_username();
        }
        $password = I('request.password', '', 'trim');
        if ($SMSopen) {
            $code        = I('request.code', '', 'trim');
            $mobile      = I('request.mobile');
            $mobile_code = session('mobile_code');
            $mobile_post = session('mobile_post');
            if (empty($code) || $code <> $mobile_code) {
                $code_err     = session('code_err');
                $code_err_is3 = session('code_err_is3');
                $code_err     = intval($code_err) + 1;
                if ($code_err > 3 || $code_err_is3) {
                    $ajax['status']  = 0;
                    $ajax['content'] = '验证码连续3次出错，请重新发送！';
                    session('code_err', null);
                    session('code_err_is3', 'true');
                    return $ajax;
                }
                session('code_err', $code_err);
                $ajax['status']  = 0;
                $ajax['content'] = '验证码错误，请重新输入';
                return $ajax;
            }
            if (empty($mobile) || $mobile <> $mobile_post) {
                $ajax['status']  = 0;
                $ajax['content'] = '手机号填写错误';
                return $ajax;
            }
        }
        if ($SMSopen == '0') {
            $mobile = '';
        }
        if ($SMSopen && $mobile == '')//开启短信
        {
            $ajax['status']  = 0;
            $ajax['content'] = '非法请求';
            return $ajax;
        }
        if (empty($username) || empty($password)) {
            $ajax['status']  = 0;
            $ajax['content'] = '用户名和密码不能为空';
            return $ajax;
        }
        $bind       = '';
        $bind_keyid = I('request.bind_keyid', '', 'trim');
        $bind_type  = I('request.bind_type', '', 'trim');
        if (!empty($bind_keyid) && !empty($bind_type)) {
            $bind = array('bind_keyid' => $bind_keyid, 'bind_type' => $bind_type);
        }
        $reg = D('User')->user_reg($username, $mobile,'', $password, true, $bind);
        return $reg;
    }
}